<template>
  <basic-container>
    <avue-crud
      :option="option"
      :table-loading="loading"
      :data="data"
      :page.sync="page"
      :search.sync="search"
      v-model="form"
      ref="crud"
      @search-change="searchChange"
      @search-reset="searchReset"
      @selection-change="selectionChange"
      @current-change="currentChange"
      @size-change="sizeChange"
      @refresh-change="refreshChange"
      @on-load="onLoad"
    >
      <!--      <template slot="menuRight">-->
      <!--        <user-table-config :option="option" @confirm="(val) => handleSetUserConfig(val, pageCode)"></user-table-config>-->
      <!--      </template>-->
      <template slot="menuLeft">
        <el-button type="warning" size="small" icon="el-icon-download" @click="$refs.exportComp.visible = true">导出</el-button>
      </template>
      <template slot-scope="{ row }" slot="menu">
        <el-button icon="el-icon-view" size="small" type="text" :disabled="row.isForSystem" @click="viewDetail(row)">详情</el-button>
      </template>
    </avue-crud>
    <el-dialog class="detailDialog" append-to-body :close-on-click-modal="false" title="详情" :visible.sync="handleDialog">
      <div class="titleIcon">
        <i class="el-icon-vertical-bar avue-group__icon"></i>
        <h4>基本信息</h4>
      </div>
      <avue-form v-model="basicInfoForm" :option="basicInfoOption"></avue-form>
      <div class="titleIcon">
        <i class="el-icon-vertical-bar avue-group__icon"></i>
        <h4>过站信息</h4>
      </div>
      <avue-crud v-if="handleDialog" :option="stationCrossingOption" :data="stationCrossingData"></avue-crud>
      <span slot="footer" class="dialog-footer">
        <el-button size="small" @click="handleDialog = false"><i class="el-icon-circle-close"></i><span>取 消</span></el-button>
      </span>
    </el-dialog>
    <export-comp ref="exportComp" page-api-url="mes-product/productionplanSchedule/query/page" :option="exportCompOption" :page="page" :query="query" file-name="生产进度查询">
    </export-comp>
  </basic-container>
</template>

<script>
import { getList, getOverStationInfo } from '@/api/product/productProgressQuery';
import { mapGetters } from 'vuex';
import CommonModalTemplate from '@/components/common-modal-template/common-modal-template.vue';
import { getList as getProductionlineList } from '@/api/product/productionline';
import userTableConfig from '@/components/user-table-config';
import userConfig from '@/mixins/userConfig';
import exportComp from '@/components/export-comp';
export default {
  name: 'ProductProductProgressQuery',
  components: {
    CommonModalTemplate,
    userTableConfig,
    exportComp
  },
  mixins: [userConfig()],
  data() {
    return {
      pageCode: 'PRODUCT_PROGRESS_QUERY',
      form: {},
      query: {},
      loading: true,
      page: {
        pageSize: 20,
        currentPage: 1,
        total: 0
      },
      search: {},
      selectionList: [],
      option: {
        height: 'calc(100vh - 483px)',
        calcHeight: 30,
        tip: false,
        searchShow: true,
        searchSpan: 6,
        searchMenuSpan: 12,
        labelWidth: 100,
        searchLabelWidth: 90,
        border: true,
        index: true,
        editBtn: false,
        viewBtn: false,
        selection: true,
        delBtn: false,
        addBtn: false,
        dialogClickModal: false,
        indexLabel: '序号',
        searchMenuPosition: 'right',
        columnBtn: false,
        menuWidth: 100,
        column: [
          {
            label: '工单号',
            prop: 'workOrderNo',
            search: true,
            searchOrder: 6,
            html: true,
            formatter: (val) => {
              return `<span style="cursor: pointer;color: #409eff" onclick="goWorkOrderProgress('${val.workOrderNo}')">${val.workOrderNo}</span>`;
            }
          },
          {
            label: '产品编码',
            prop: 'partNo',
            search: true,
            searchOrder: 3
          },
          {
            label: '产品型号',
            prop: 'partModel',
            search: true,
            searchOrder: 4
          },
          {
            label: '客户',
            prop: 'partnerName',
            search: true,
            searchOrder: 5
          },
          {
            label: '车间',
            prop: 'workshopId',
            search: true,
            searchOrder: 1,
            type: 'select',
            dicUrl: '/api/mes-admin/workshop/key-value',
            props: {
              label: 'workshopName',
              value: 'workshopId'
            }
          },
          {
            label: '产线',
            prop: 'lineId',
            search: true,
            searchOrder: 2,
            type: 'select',
            dicData: [],
            hide: true
          },
          {
            label: '产线',
            prop: 'lineName'
          },
          {
            label: '计划完成时间',
            prop: 'planEndDate',
            format: 'yyyy-MM-dd HH:mm:ss'
          },
          {
            label: '工单计划数量',
            prop: 'planQuantity'
          },
          {
            label: '工单完工数量',
            prop: 'completedQuantity'
          },
          {
            label: '工单达成率',
            prop: 'achieveRate'
          },
          {
            label: '合格率',
            prop: 'passRate'
          },
          {
            label: '不良数',
            prop: 'ngQuantity'
          }
        ]
      },
      data: [],
      handleDialog: false,
      basicInfoForm: {},
      basicInfoOption: {
        submitBtn: false,
        emptyBtn: false,
        labelWidth: 150,
        detail: true,
        column: [
          {
            label: '工单号',
            prop: 'workOrderNo'
          },
          {
            label: '工单计划开始时间',
            prop: 'planStartDate'
          },
          {
            label: '产品名称',
            prop: 'partName'
          },
          {
            label: '工单计划结束时间',
            prop: 'planEndDate'
          },
          {
            label: '产品编码',
            prop: 'partNo'
          },
          {
            label: '工单计划数量',
            prop: 'planQuantity'
          },
          {
            label: '产品型号',
            prop: 'partModel'
          },
          {
            label: '车间',
            prop: 'workshopName'
          },
          {
            label: '客户',
            prop: 'partnerName'
          },
          {
            label: '产线',
            prop: 'lineName'
          }
        ]
      },
      stationCrossingData: [],
      stationCrossingOption: {
        height: 300,
        calcHeight: 30,
        tip: false,
        border: true,
        index: true,
        editBtn: false,
        viewBtn: false,
        selection: false,
        delBtn: false,
        addBtn: false,
        indexLabel: '序号',
        columnBtn: false,
        refreshBtn: false,
        menu: false,
        column: [
          {
            label: '工序名称',
            prop: 'routeName'
          },
          {
            label: '工序类型',
            prop: 'routeType',
            type: 'select',
            dicUrl: '/api/blade-system/dict/dictionary?code=station_type',
            props: {
              label: 'dictValue',
              value: 'dictKey'
            }
          },
          {
            label: '已过站数量',
            prop: 'passQuantity'
          },
          {
            label: '未过站数量',
            prop: 'waitingQuantity'
          },
          {
            label: '合格率',
            prop: 'passRate'
          },
          {
            label: '不良数',
            prop: 'ngQuantity'
          }
        ]
      },
      exportCompOption: {}
    };
  },
  computed: {
    ...mapGetters(['permission']),
    ids() {
      let ids = [];
      this.selectionList.forEach((ele) => {
        ids.push(ele.id);
      });
      return ids.join(',');
    }
  },
  watch: {
    'search.workshopId'(val) {
      this.search.lineId = '';
      const column = this.findObject(this.option.column, 'lineId');
      column.dicData = [];
      if (val) {
        getProductionlineList(1, 10000, { workshopId: val })
          .then((res) => {
            column.dicData = this._get(res, 'data.data.records', []);
            column.dicData = this._map(column.dicData, (item) => {
              return {
                label: item.name,
                value: item.id
              };
            });
          })
          .catch((err) => {
            column.dicData = [];
          });
      }
    }
  },
  created() {
    window.goWorkOrderProgress = this.goWorkOrderProgress;
    this.exportCompOption = this._cloneDeep(this.option);
    this.exportCompOption.column.splice(4, 1, {
      label: '车间',
      prop: 'workshopId'
    });
  },
  mounted() {
    this.$nextTick(() => {
      this.$structuralSplit();
    });
    const column = this.findObject(this.option.column, 'lineId');
    column.dicData = [];
  },
  methods: {
    searchReset() {
      this.search = {};
      this.query = {};
      this.onLoad(this.page);
    },
    searchChange(params, done) {
      this.query = params;
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
      done();
    },
    selectionChange(list) {
      this.selectionList = list;
    },
    selectionClear() {
      this.selectionList = [];
      this.$refs.crud.toggleSelection();
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },
    refreshChange() {
      this.onLoad(this.page, this.query);
    },
    onLoad(page, params = {}) {
      this.loading = true;
      getList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then((res) => {
        const data = res.data.data;
        this.page.total = data.total;
        this.data = data.records;
        this._forEach(this.data, (item) => {
          item.achieveRate = this._cloneDeep(item.achieveRate) + '%';
          item.passRate = this._cloneDeep(item.passRate) + '%';
        });
        this.loading = false;
        this.selectionClear();
      });
    },

    viewDetail(row) {
      this.handleDialog = true;
      this.basicInfoForm = row;
      getOverStationInfo(1, 10000, { workOrderNo: row.workOrderNo, flag: true }).then(
        (res) => {
          this.stationCrossingData = this._get(res, 'data.data.records', []);
          this._forEach(this.stationCrossingData, (item) => {
            item.passRate = this._cloneDeep(item.passRate) + '%';
          });
        },
        (error) => {
          this.stationCrossingData = [];
        }
      );
    },
    goWorkOrderProgress(workOrderNo) {
      this.$router.replace({ path: '/product/workOrderProgress', query: { workOrderNo: workOrderNo } });
    }
  }
};
</script>

<style lang="scss" scoped>
.titleIcon {
  display: flex;
  align-items: center;
}
.detailDialog {
  ::v-deep .el-card__body {
    .avue-crud__menu {
      display: none !important;
    }
  }
}
</style>
